#!/usr/bin/env python
import Tkinter as tk

NUM_LEDS = 8
DIAM_LED = 20
DIST_LEDS = 30
X0 = Y0 = DIST_LEDS
LARG_TELA = 640
ALT_TELA = 480
DELTA_X = 5
COLS_POR_PIX = 4

glifo = '''
XXXXXX..
X.....X.
X.....X.
XXXXXX..
X..X....
X...X...
X....X..
X.....X.
'''

glifo = glifo.strip().split()

LARG_GLIFO = len(glifo[0])
colunas = []
for col in range(LARG_GLIFO):
    coluna = []
    for lin in range(len(glifo)):
        bit = (glifo[lin][col] == 'X') and 1 or 0 
        coluna.append(bit)
    colunas.append(coluna)
    
jan = tk.Tk()

tela = tk.Canvas(jan, background='black',width=LARG_TELA,height=ALT_TELA)
tela.pack()

leds = []
for i in range(NUM_LEDS):
    x = X0
    y = Y0 + DIST_LEDS * i
    leds.append(tela.create_oval(x,y,x+DIAM_LED,y+DIAM_LED,
                                 fill='red',tag='led%x'%i))

vx = - DELTA_X
def animar():
    global leds, vx, ciclo
    x1, y1, x2, y2 = tela.coords(leds[0])
    col = int((x1-X0) / DELTA_X)
    col_glifo = int(float(col)/COLS_POR_PIX) % LARG_GLIFO
    for i in range(NUM_LEDS):
        if colunas[col_glifo][i]:
            tela.itemconfig(leds[i],fill='red')
        else:
            tela.itemconfig(leds[i],fill='black')
            
    # print col_glifo,
    '''
    # vai e volta
    if ((x1 > (LARG_TELA-DIST_LEDS) and vx > 0) or 
        (x1 < DIST_LEDS and vx < -0)):
        vx = -vx    
        # raise SystemExit
    '''
    '''
    # so vai
    if x1 > (LARG_TELA-DIST_LEDS):
        for i in range(NUM_LEDS):
            x = X0
            y = Y0 + DIST_LEDS * i
            tela.coords('led%x'%i,x,y,x+DIAM_LED,y+DIAM_LED)
    '''
    # so volta
    if x1 < -DIST_LEDS:
        for i in range(NUM_LEDS):
            x = LARG_TELA-DIST_LEDS
            y = Y0 + DIST_LEDS * i
            tela.coords('led%x'%i,x,y,x+DIAM_LED,y+DIAM_LED)
    
    tela.move(tk.ALL,vx,0)
    jan.after(2,animar)

animar()
jan.mainloop()
